package com.dameng.jianyouquan.interviewer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.interviewer.ChooseCity.ChooseCityActivity;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InterviewerPublish4Activity extends AppCompatActivity implements Inputtips.InputtipsListener, CalendarView.OnMonthChangeListener, CalendarView.OnCalendarRangeSelectListener, CalendarView.OnCalendarInterceptListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_city)
    EditText etCity;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.calendarView)
    CalendarView mCalendarView;
    private String str_city;

    private List<Tip> list = new ArrayList<>();
    private Myadapter myadapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interviewer_publish4);
        ButterKnife.bind(this);
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(InterviewerPublish4Activity.this, ChooseCityActivity.class);
                startActivityForResult(intent, 10);
            }
        });
        mCalendarView.setOnCalendarRangeSelectListener(this);
        mCalendarView.setOnMonthChangeListener(this);
        //设置日期拦截事件，当前有效
        mCalendarView.setOnCalendarInterceptListener(this);
//        int mCalendarHeight = DensityUtil.dp2px(this, 46);
        mCalendarView.setRange(2000, 1, 1,
                mCalendarView.getCurYear(), mCalendarView.getCurMonth(), mCalendarView.getCurDay()
        );
        mCalendarView.post(new Runnable() {
            @Override
            public void run() {
                mCalendarView.scrollToCurrent();
            }
        });

        myadapter = new Myadapter();
        lv.setAdapter(myadapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Tip tip = list.get(i);
                String address = tip.getAddress() + tip.getName();
                etCity.setText(address);
                list.clear();
                myadapter.notifyDataSetChanged();
            }
        });
        EditText et_city = findViewById(R.id.et_city);
        et_city.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String newText = editable.toString().trim();
                InputtipsQuery inputquery;
                if (TextUtils.isEmpty(str_city)) {
                    inputquery = new InputtipsQuery(newText, "郑州");
                } else {
                    inputquery = new InputtipsQuery(newText, str_city);
                }

                inputquery.setCityLimit(true);//限制在当前城市
                Inputtips inputTips = new Inputtips(InterviewerPublish4Activity.this, inputquery);
                inputTips.setInputtipsListener(InterviewerPublish4Activity.this);
                inputTips.requestInputtipsAsyn();
            }
        });

    }



    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        this.list = list;
        myadapter.notifyDataSetChanged();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 3) {
            assert data != null;
            str_city = data.getStringExtra("city");
            tvCity.setText(str_city);
        }
    }


    private class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(viewGroup.getContext());
            textView.setText(list.get(i).getDistrict() + list.get(i).getName());
            return textView;
        }
    }

    @Override
    public void onCalendarSelectOutOfRange(Calendar calendar) {

    }

    @Override
    public void onSelectOutOfRange(Calendar calendar, boolean isOutOfMinRange) {

    }

    @Override
    public void onCalendarRangeSelect(Calendar calendar, boolean isEnd) {

    }


    @Override
    public void onMonthChange(int year, int month) {

    }


    @Override
    public boolean onCalendarIntercept(Calendar calendar) {
        return false;
    }

    @Override
    public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {

    }


}
